package com.zhucan.stream.producer;

import com.zhucan.stream.binder.OrderStreamBinder;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;

/**
 * @author zhuCan
 * @email zhucan@co-mall.com
 * @description
 * @since 2020/8/20 9:12
 */
@Slf4j
@Component
@AllArgsConstructor
public class OrderProducer {

  private OrderStreamBinder streamBinder;

  public boolean publish(@Payload Object data){
    log.info("publish msg: {}", data);
    return streamBinder.producer().send(
        MessageBuilder.withPayload(data).build()
    );
  }

  public boolean publishHeader(@Payload Object data){
    log.info("publishHeader msg: {}", data);
    return streamBinder.producer().send(
        MessageBuilder
            .withPayload(data)
            .setHeader("destination", "route")
            .build()
    );
  }

  public boolean publishRoute(@Payload Object data){
    log.info("publishRoute msg: {}", data);
    return streamBinder.producerRoute().send(
        MessageBuilder
            .withPayload(data)
            .setHeader("destination", "order-key")
            .build()
    );
  }

  public boolean publishRouteTest(@Payload Object data){
    log.info("publishRouteTest msg: {}", data);
    return streamBinder.producerRouteTest().send(
        MessageBuilder
            .withPayload(data)
            .setHeader("destination", "order-key-test")
            .build()
    );
  }

}
